﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using University.Workshop.Business;
using University.Workshop.Model;
using System.Text.RegularExpressions;

namespace University.Workshop.Presenters
{
    public class PresenterProvider
    {
           private readonly IProvider View;

           public PresenterProvider(IProvider view)
        {
            View = view;
        }

           public DataTable Search()
           {
                   //validate inputs
                   if (string.IsNullOrEmpty(this.View.Search))
                   {
                       throw new BusinessException("El campo de busqueda esta vacio!!!");
                   }

                   if (!(Regex.IsMatch(this.View.Search, @"^[A-Za-z0-9 .]*$")))
                   {
                       throw new BusinessException("El formato ingresado es invalido");
                   }
                   if ((this.View.Search.Length > 12))
                   {
                       throw new BusinessException("El tamaño de la palabra ingresado es invalido");
                   }
                   return GetProviders();

           }

           private DataTable GetProviders()
           {
               return ProviderControl.SearchProvider(View.Search, !View.Active);
           }

           public bool SetProvider() {
               Provider prov = ProviderControl.GetProviderByNit(this.View.ModifyNit);
                
               return SetProvider(prov);
           }
           private bool SetProvider(Provider provider) {
               this.View.ModifyCellular = provider.Cellular;
               this.View.ModifyAddress = provider.Address;
               this.View.ModifyContact = provider.Contact;
               this.View.ModifyEmail = provider.Email;
               this.View.ModifyName = provider.Name;
               this.View.ModifyNit = provider.Nit;
               this.View.ModifyTelephone = provider.Telephone;
               this.View.ModifyWeb = provider.Web;
               return true;
           }
           private void VerifyAttribsProvider()
           {
               #region Verificar Campos Obligatorios
               if (!((Regex.IsMatch(this.View.ModifyName, @"^[A-Za-z0-9 .]*$")) && this.View.ModifyName.Length < 41 && this.View.ModifyName.Length > 0))
               {
                   throw new BusinessException("Nombre es Invalido");
               }
               if (!((Regex.IsMatch(this.View.ModifyAddress, @"^[A-Za-z0-9 .#]*$")) && this.View.ModifyAddress.Length < 41 && this.View.ModifyAddress.Length > 0))
               {
                   throw new BusinessException("Direccion es Invalida");
               }
               if (string.IsNullOrEmpty(this.View.ModifyTelephone) && string.IsNullOrEmpty(this.View.ModifyCellular))
               {
                   throw new BusinessException("Porfavor Ingrese un celular o telefono");
               }
               if ((!string.IsNullOrEmpty(this.View.ModifyTelephone)) && ((this.View.ModifyTelephone.Length != 7) || (!Regex.IsMatch(this.View.ModifyTelephone, "^[0-9]*$"))))
               {
                   throw new BusinessException("Telefono es Invalido");
               }
               if ((!string.IsNullOrEmpty(this.View.ModifyCellular)) && ((this.View.ModifyCellular.Length != 8) || (!Regex.IsMatch(this.View.ModifyCellular, "^[0-9]*$"))))
               {
                   throw new BusinessException("Celular es Invalido");
               }
               #endregion
               #region Verificar Campos Opcionales
               if ((!string.IsNullOrEmpty(this.View.ModifyContact)) && ((this.View.ModifyContact.Length > 41) || (!((Regex.IsMatch(this.View.ModifyContact, @"^[A-Za-z0-9 .]*$"))))))
               {
                   throw new BusinessException("Nombre del Contacto es Invalido");
               }
               if ((!string.IsNullOrEmpty(this.View.ModifyEmail)) && ((this.View.ModifyEmail.Length > 41) || (!((Regex.IsMatch(this.View.ModifyEmail, @"^([\w-]+\.)*?[\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$"))))))
               {
                   throw new BusinessException("Email es Invalido");
               }
               if ((!string.IsNullOrEmpty(this.View.ModifyWeb)) && ((this.View.ModifyWeb.Length > 41) || (!((Regex.IsMatch(this.View.ModifyWeb, @"^([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$"))))))
               {
                   throw new BusinessException("Web es Invalido",null);
               }
               #endregion
           }   
        public bool Update() {
            this.VerifyAttribsProvider();           
              
               Provider prov = new Provider() {Address = this.View.ModifyAddress, Cellular=this.View.ModifyCellular,Contact=this.View.ModifyContact,Email=this.View.ModifyEmail,Name=this.View.ModifyName,Nit=this.View.ModifyNit,Telephone=this.View.ModifyTelephone,Web=this.View.ModifyWeb };

               return Update(prov);
           }
           private static bool Update(Provider provider) {
               return ProviderControl.UpdateProvider(provider);
           }

        

           public bool DeleteProvider()
           {
               ProviderControl.DeleteProviderControl(View.ModifyNit);
               return true;
           }

           public bool EnableProvider()
           {
               ProviderControl.EnableProviderControl(View.ModifyNit);
               return true;
           }
    }
}
